﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Ensemble;
using Ensemble2D.Properties;
using WeifenLuo.WinFormsUI.Docking;
using Form = System.Windows.Forms.Form;

namespace Ensemble2D.Forms
{
    public partial class MainForm : Form
    {
        private bool nonUserClosing = false;
        private bool isTest = true;
        public MainForm()
        {
            InitializeComponent();

            ApplyDockPanelSkin();
            InitMenus();
        }

        private void ApplyDockPanelSkin()
        {
            var darkTheme = new VS2015DarkTheme();
            dockPanel.Theme = darkTheme;
        }

        private void InitMenus()
        {
            /*--------------------File-----------------------*/
            var toolStripItem = new ToolStripMenuItem()
            {
                Text = Resources.MenuName_File,
            };
            mainMenuStrip.Items.Add(toolStripItem);

            var newProject = new ToolStripButton()
            {
                Text = Resources.MenuName_NewProject,
            };
            newProject.Click += newProject_Click;
            toolStripItem.DropDownItems.Add(newProject);
            /*----------------------Run---------------------*/
            var runToolStripItem = new ToolStripMenuItem()
            {
                Text = Resources.MenuName_Run,
            };
            mainMenuStrip.Items.Add(runToolStripItem);

            var runAppToolStripItem = new ToolStripMenuItem()
            {
                Text = Resources.MenuName_RunGame,
            };
            runAppToolStripItem.Click += runAppToolStripItem_Click;
            runToolStripItem.DropDownItems.Add(runAppToolStripItem);
            /*----------------------Run---------------------*/
            if (isTest)
            {
                var testToolStripItem = new ToolStripMenuItem()
                {
                    Text = Resources.MenuName_Test,
                };
                testToolStripItem.Click += testAppToolStripItem_Click;
                mainMenuStrip.Items.Add(testToolStripItem);
            }
        }

        private void testAppToolStripItem_Click(object sender, EventArgs e)
        {

        }

        private void runAppToolStripItem_Click(object sender, EventArgs e)
        {
            var startInfo = new ProcessStartInfo()
            {
                FileName = Path.GetFullPath(EnsembleEditorApp.LauncherAppPath),
                Arguments = EnsembleApp.CmdArgEditor + " " + EnsembleApp.CmdArgDebug,
                WorkingDirectory = Environment.CurrentDirectory,
            };
            var appProc = Process.Start(startInfo);
        }

        private void newProject_Click(object sender, EventArgs e)
        {
            var newProject = new NewProjectDialog();
            var result = newProject.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                var startInfo = new ProcessStartInfo(newProject.ResultEditorBinary);
                startInfo.WorkingDirectory = Path.GetDirectoryName(startInfo.FileName);
                startInfo.UseShellExecute = false;
                Process.Start(startInfo);

                CloseNonUser();
            }
        }

        public void CloseNonUser()
        {
            nonUserClosing = true;
            Close();
            nonUserClosing = false;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            WindowState = FormWindowState.Maximized;
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            Application.Exit();
        }
    }
}
